#include <dspSimulator/command/printCReg.h>
#include <dspSimulator/dspSimulator/dspSimulator.h>


void PrintCRegCommand::execute(std::string command) {
    auto dspSimulator = dynamic_cast<DSPSimulator *>(m_simulator);
    if (dspSimulator == nullptr) {
        fatalMacro("该模拟器对象不为dsp模拟器对象!");
    }
    auto targetMachine = dspSimulator->m_targetMachine;
    auto dspMachine = std::dynamic_pointer_cast<DspMachine>(targetMachine);
    if (dspMachine == nullptr) {
        fatalMacro("该目标机对象不为dsp目标机对象!");
    }

    if (command.size() < 6) {
        requestReEnter;
        return;
    }
    std::string cRegName = command.substr(5);
    auto creg = dspMachine->getRegisters()->getReg32ByName(cRegName);
    if (!creg) {
        fprintf(stdout, "不存在的控制寄存器\n");
        return;
    }
    fprintf(stdout, "%s : %08X\n", cRegName.c_str(), *creg->getDataPtr());
}

std::shared_ptr<PrintCRegCommand> PrintCRegCommand::get(Simulator* dspSimulator) {
    static std::shared_ptr<PrintCRegCommand> printCRegCommand;
    if (printCRegCommand == nullptr) {
        printCRegCommand = std::make_shared<PrintCRegCommand>(dspSimulator);
    }
    return printCRegCommand;
}
